<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="柜子编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="str">
              <a-input v-model="model.str" placeholder="请输入柜子编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="柜子名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="str1">
              <a-input v-model="model.str1" placeholder="请输入柜子名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工具箱id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="toolboxId">
<!--              <j-dict-select-tag type="list" v-model="model.toolboxId" dictCode="" placeholder="请选择工具箱id" />-->
              <a-select v-model="model.str2" ref="toolboxId" @change="getToolValue">
                <a-select-option v-for="tool in toolbox" :key="tool.key" :value="tool.id">
                  {{tool.str1}}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'CabinetManageForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        toolbox:{},
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           str: [
              { required: true, message: '请输入柜子编号!'},
              { validator: (rule, value, callback) => validateDuplicateValue('cabinet_manage', 'cabinet_code', value, this.model.id, callback)},
           ],
           str1: [
              { required: true, message: '请输入柜子名称!'},
              { validator: (rule, value, callback) => validateDuplicateValue('cabinet_manage', 'cabinet_name', value, this.model.id, callback)},
           ],
           toolboxId: [
              { required: false},
              { validator: (rule, value, callback) => validateDuplicateValue('cabinet_manage', 'toolbox_id', value, this.model.id, callback)},
           ],
        },
        url: {
          add: "/cabinetmanage/cabinetManage/add",
          edit: "/cabinetmanage/cabinetManage/edit",
          queryById: "/cabinetmanage/cabinetManage/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    mounted() {
      this.getToolbox();
    },
    methods: {
      getToolbox(){
        getAction('/toolbox/toolbox/list',null).then(res => {
          this.toolbox = res.result.records
          console.log("toolbox")
          console.log(this.toolbox)
        }).catch(err =>{
          this.$message.error("获取工具失败  ")
        })
      },
      getToolValue(value){
        this.model.toolboxId = value;
        console.log("value"+value)
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            console.log(this.model)
            this.model.cabinetCode = this.model.str
            this.model.cabinetName = this.model.str1
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>